package com.darly.book.presenter;

import com.darly.book.model.flag.BookDistillate;
import com.darly.book.model.flag.BookSort;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.DiscHelpsContract;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.chinese.db.book.table.BookHelpsBean;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class DiscHelpsPresenter extends RxPresenter<DiscHelpsContract.View> implements DiscHelpsContract.Presenter {
    private boolean isLocalLoad = false;

    @Override
    public void firstLoading(BookSort sort, int start, int limited, BookDistillate distillate) {
        //获取数据库中的数据
//        Single<List<BookHelpsBean>> localObserver = LocalRepository.getInstance()
//                .getBookHelps(sort.getDbName(), start, limited, distillate.getDbName());
        Single<List<BookHelpsBean>> remoteObserver = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookHelpList("all", sort.getNetName(), start + "", limited + "", distillate.getNetName())
                .map((listBean) -> listBean.getHelps());

//        Single.concat(localObserver, remoteObserver)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (beans) -> {
//                            mView.finishRefresh(beans);
//                        }
//                        ,
//                        (e) -> {
//                            isLocalLoad = true;
//                            mView.complete();
//                            mView.showErrorTip();
//                            DLog.e(e);
//                        }
//                        ,
//                        () -> {
//                            isLocalLoad = false;
//                            mView.complete();
//                        }
//                );
    }

    @Override
    public void refreshBookHelps(BookSort sort, int start, int limited, BookDistillate distillate) {
        Disposable refreshDispo = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookHelpList("all", sort.getNetName(), start + "", limited + "", distillate.getNetName())
                .map((listBean) -> listBean.getHelps())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            isLocalLoad = false;
                            mView.finishRefresh(beans);
                            mView.complete();
                        }
                        ,
                        (e) -> {
                            mView.complete();
                            mView.showErrorTip();
                            DLog.e(e);
                        }
                );
        addDisposable(refreshDispo);
    }

    @Override
    public void loadingBookHelps(BookSort sort, int start, int limited, BookDistillate distillate) {
        if (isLocalLoad) {
//            Single<List<BookHelpsBean>> single = LocalRepository.getInstance()
//                    .getBookHelps(sort.getDbName(), start, limited, distillate.getDbName());
//            loadBookHelps(single);
        } else {
            Single<List<BookHelpsBean>> single = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                    .getBookHelpList("all", sort.getNetName(), start + "", limited + "", distillate.getNetName())
                    .map((listBean) -> listBean.getHelps());
            loadBookHelps(single);
        }
    }

    @Override
    public void saveBookHelps(List<BookHelpsBean> beans) {
//        LocalRepository.getInstance()
//                .saveBookHelps(beans);
    }

    private void loadBookHelps(Single<List<BookHelpsBean>> Single) {
        Disposable loadDispo = Single
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            mView.finishLoading(beans);
                        }
                        ,
                        (e) -> {
                            mView.showError();
                            DLog.e(e);
                        }
                );
        addDisposable(loadDispo);
    }
}
